var click_method = '',
	goods_sku = getParam('goods_sku'),
	judge = getParam('judge'),
	cart_id = getParam('cart_id'),
	member_id = $.cookie('member_id'),
	get_category_list = goods_detail(goods_sku),
	judge_star = '';
page = {
	'page': 1,
	'limit': 10
};

function set_goods_price_record(data) {
	var ul = $('.Landing_price_style');
	ul.html('');
	if(data.data.length == 0) {
		ul.html('暂无历史记录');
	} else {
		for(var i in data.data) {
			var e = data.data[i],
				li = '<p>' + e.create_time + '：<b>' + e.price + '</b></p>';
			ul.append(li);
		}
	}
}

function create_goods_detail(data) {
	if(data.code != 1) {
		layer.msg('参数错误', {
			icon: 2,
			anim: 6
		});
		location.href = 'goods_list.html';
		return false;
	}
	if(judge != 'yes') {
		$('.commentBox').remove();
	}
	var goods_category_link_str = '<em></em><a href="/">首页</a> < <a href="goods_list.html?category_sign1=' + data.data.category_sign1 + '">' + data.data.category_name1 + '</a>' +
		' < <a href="goods_list.html?category_sign2=' + data.data.category_sign2 + '">' + data.data.category_name2 + '</a>' +
		' < <a href="goods_list.html?category_sign3=' + data.data.category_sign3 + '">' + data.data.category_name3 + '</a>' +
		' < 产品详情';
	$('.Location_link').html(goods_category_link_str);
	$('.judge-li span').html(data.data.judge_count);
	$('.Product_title .goods_name').html(data.data.goods_name);
	$('.Product_title .goods_model').html(data.data.goods_model);
	$('.cars_price .sale_price').html(data.data.sale_price+'/'+data.data.goods_unit);
	$('.inventory .view_stock').html(data.data.sale_price_info.data[0]['view_stock']);
	$('.market_price').html(data.data.market_price+'/'+data.data.goods_unit);
	$('.danwei').html(data.data.goods_unit);
	$('.goods_content').html(data.data.goods_content);
	var goods_attr_ul = $('.goods_attr'),
		li = '<table width="100%" border="0" cellspacing="0" cellpadding="0" class="parameter_style">',
		group_name = '';
	goods_attr_ul.html('');
	for(var i in data.data.goods_attr) {
		var e = data.data.goods_attr[i];
		if(e.group_name != group_name) {
			li = li + '<tr><th colspan="2">' + e.group_name + '</th></tr>';
			group_name = e.group_name;
		}
		li = li + '<tr><td class="lable_name">' + e.attr_name + '</td><td>' + e.attr_value + '</td></tr>';

	}
	li = li + '</table>';
	goods_attr_ul.html(li);
	var imgUl = $('#imageMenu ul');
	imgUl.html('');
	// for(var i in data.data.goods_pictures) {
	// 	var e = data.data.goods_pictures[i],
	// 		li = '<li><img src="' + e.save_path + '" width="68" height="68" /></li>';
	// 	imgUl.append(li);
	// }
	var li = '<li><img src="' + data.data.goods_picture.save_path + '" width="68" height="68" /></li>';
	imgUl.append(li);
	imgScroll();
	// 品牌信息
	$('.logo_Brand').html('<img style="width:120px" src="' + data.data.brand_info.brand_picture_info.save_path + '" title="' + data.data.brand_name + '" /><p>' + data.data.brand_name + '</p>')
	$('.xs_species').html('产品数：' + data.data.brand_goods_count);
	// 品牌下的列表
	var ul = $('.brand_car_ul');
	ul.html('');
	for(var i in data.data.brand_goods_list) {
		var e = data.data.brand_goods_list[i],
			li = '<li class="gl-item Brand_car_list">' +
			'	<div class="Borders" style="margin:30px 10px 30px 10px;">' +

			'		<div class="Price"><em>￥</em><b>' + e.sale_price_info.min_sale_price + '</b><span>' + e.sale_price_info.min_market_price + '</span></div>' +
			'		<div class="name">' +
			'			<a href="goods_detail.html?goods_sku=' + e.goods_sku + '" title="+ e.goods_name +">' + e.goods_name + '</a>' +
			'		</div>' +
			'		<div class="carbox-tip">' + e.goods_model + '</div>' +
			'		<div class="Review">已有' +
			'			<a href="goods_detail.html?goods_sku=' + e.goods_sku + '">' + e.sale_num + '</a>购买记录</div>' +
			'	</div>' +
			'</li>';
		ul.append(li);
	}
};

function add_cart_success(data) {
	console.info(data);
	console.info(click_method);
	if(data.code == 1) {
		if(click_method == 'buyBtn') {
			location.href = 'cart.html';
		} else {
			shopping_cart_btn();
		}
	} else {
		layer.msg(data.msg, {
			icon: 2,
			anim: 6
		});
	}
}

function send_judge_return(data) {
	if(data.code == 0) {
		layer.msg(data.msg, {
			icon: 2,
			anim: 6
		});
	} else {
		layer.msg('评论发布成功', {
			icon: 1,
			anim: 6
		});
		$('.commentBox').remove();
		$('.all-judge').trigger('click');
	}
}

function create_judge_list_html(data) {
	if(data.code == 0) {
		layer.msg(data.msg, {
			icon: 2,
			anim: 6
		});
	}
	$('.all-judge span').html(data.data.sum.all);
	$('.judge-li span').html(data.data.sum.all);
	$('.good-judge span').html(data.data.sum.good);
	$('.just-judge span').html(data.data.sum.just);
	$('.bad-judge span').html(data.data.sum.bad);
	$('.good-rate').html(data.data.sum.good_rate);
	$('.just-rate').html(data.data.sum.just_rate);
	$('.bad-rate').html(data.data.sum.bad_rate);
	$('.good-rate-line').css('width', data.data.sum.good_rate + 'px');
	$('.just-rate-line').css('width', data.data.sum.just_rate + 'px');
	$('.bad-rate-line').css('width', data.data.sum.bad_rate + 'px');
	var judge_ul = $('.judge-ul');
	var page_box = $('.Paging');
	layui.use('laypage', function() {
		var laypage = layui.laypage;
		laypage.render({
			elem: 'Paging',
			limit: page.limit,
			curr: page.page,
			count: data.count,
			first: '首页',
			last: '尾页',
			groups: 5,
			theme: '#0c83c2',
			layout: ['prev', 'page', 'next', 'pages', 'skip'],
			jump: function(obj, first) {
				if(!first) {
					page.page = obj.curr;
					judge_list(goods_sku, judge_star, page.page, page.limit);
				}
			}
		});
	});
	$('.s_Paging').on('click', '.on', function() {
		if(page.page == $(this).attr('data-page')) {
			return false;
		}
		page.page = $(this).attr('data-page');
		judge_list(goods_sku, judge_star, page.page, page.limit);
	});
	judge_ul.html('');
	for(var i in data.data.list) {
		var e = data.data.list[i],
			username = e.member_info.realname ? e.member_info.realname : e.member_info.username,
			li = '<li class="Comments_content">' +
			'	<div class="username">' +
			'		<h5>' + username + '</h5>' +
			'		<div class="score"><b style="width:' + (e.judge_star * 20) + '%;"></b></div>' +
			'	</div>' +
			'	<div class="commentC">' + e.judge_content +
			'		<span class="commenttime">发表时间：' + e.create_time + '</span>' +
			'	</div>' +
			'</li>';
		judge_ul.append(li);
	}
}

function view_judge_form(data) {
	if(data.code == 1) {
		if(data.count == 1) {
			$('.commentBox').remove();
		} else {
			$('.commentBox').show();
		}
	} else {
		$('.commentBox').remove();
	}

}
$(function() {
	check_judge_commit(member_id, goods_sku, cart_id);
	judge_list(goods_sku, judge_star, page.page, page.limit);

	goods_price_record(goods_sku, 'get_sale_price', set_goods_price_record);
	$('.judge-tab').on('click', 'li', function() {
		$('.judge-tab li').removeClass('active');
		$(this).addClass('active');
		judge_star = $(this).attr('data-star');
		page.page = 1;
		judge_list(goods_sku, judge_star, page.page, page.limit);
	});
	if(judge == 'yes') {
		$('.judge-li').trigger('click');
	}
	var num_input = $('#buy_num');
	$('.btn-reduce').on('click', function() {
		if(parseInt(num_input.val()) > 1)
			num_input.val(parseInt(num_input.val()) - 1);
	})
	$('.btn-add').on('click', function() {
		if(parseInt(num_input.val()) < parseInt($('.view_stock').html()))
			num_input.val(parseInt(num_input.val()) + 1);
	})
	num_input.on('input', function() {
		if(isNaN(num_input.val()))
			num_input.val('1');
		if(parseInt(num_input.val()) > parseInt($('.view_stock').html()))
			num_input.val($('.view_stock').html());
	});
	$('.buy-goods').on('click', function() {
		var member_id = $.cookie('member_id');

		if(!member_id) {
			location.href = 'login.html';
			return false;
		}
		click_method = $(this).attr('id');
		add_cart(goods_sku, $('#buy_num').val(), member_id);
	});
})
layui.use(['form', 'jquery', 'layer', 'rate'], function() {
	var form = layui.form;
	var $ = layui.$;
	var layer = layui.layer;
	var rate = layui.rate;
	var ins1 = rate.render({
		elem: '#stars',
		value: 5,
		theme: '#FF8000',
		text: true,
		setText: function(value) {
			var arrs = {
				'1': '极差',
				'2': '差评',
				'3': '中评',
				'4': '好评',
				'5': '极好'
			};
			this.span.text(arrs[value] || (value + "星"));
		},
		choose: function(value) {
			$('#star').val(value);
		}
	});
	form.on('submit(submit_judge)', function(data) {
		if(!member_id) {
			location.href = 'login.html';
		}
		data.field.goods_sku = goods_sku;
		data.field.cart_id = cart_id;
		send_judge(member_id, data.field);
	})
})